package com.heyqing.prepare.bitwise;

import com.heyqing.prepare.Tools;

/**
 * ClassName:TestEntry
 * Package:com.heyqing.prepare.bitwise
 * Description:
 * 位运算测试入口
 *
 * @Date:2024/7/21
 * @Author:Heyqing
 */
public class BitwiseOperationTestEntry {

    public static void main(String[] args) {
        oddOrEvenTest();
        findSingularNumberTest();
        findPairNumberTest();
        findBinary1CountsTest();
        isPowerOf2Test();
        OddAndEvenSwapTest();
    }

    /**
     * 奇偶位互换
     */
    private static void OddAndEvenSwapTest() {
        //需要进行交换的数 10 01 01 即十进制37
        int number = 37; //TODO 转换完 01 10 10 即十进制26
        Integer oddAndEvenSwap = BitwiseOperation.OddAndEvenSwap(number);
        Tools.print("奇偶位互换",number,"转换后的数字为：" + oddAndEvenSwap);
    }

    /**
     * 2的整数幂
     */
    private static void isPowerOf2Test() {
        //需要判断的数字
        int number = 128;
        Boolean powerOf2 = BitwiseOperation.isPowerOf2(number);
        String res = "";
        if (powerOf2) {
            res = number + "是2的整数幂";
        } else {
            res = number + "不是2的整数幂";
        }
        Tools.print("2的整数幂", number, res);
    }

    /**
     * 二进制数中1的个数
     */
    private static void findBinary1CountsTest() {
        //需要计算的10进制数86 即1010110
        int number = 86; //TODO 1的个数为4
        Integer binary1CountsByMove1 = BitwiseOperation.findBinary1CountsByMove1(number);
        Tools.print("二进制数中1的个数-移动1", number, "1的个数为：" + binary1CountsByMove1);
        Integer binary1CountsByMoveNumber = BitwiseOperation.findBinary1CountsByMoveNumber(number);
        Tools.print("二进制数中1的个数-移动数字", number, "1的个数为：" + binary1CountsByMoveNumber);
        Integer binary1CountsBySubtraction1 = BitwiseOperation.findBinary1CountsBySubtraction1(number);
        Tools.print("二进制数中1的个数-减1", number, "1的个数为：" + binary1CountsBySubtraction1);
    }

    /**
     * 奇偶数判断测试
     */
    private static void oddOrEvenTest() {
        //需要判断的数
        int number = 9;
        Boolean isOdd = BitwiseOperation.OddOrEven(number);
        String res = "";
        if (isOdd) {
            res = number + "为奇数";
        } else {
            res = number + "为偶数";
        }
        Tools.print("奇偶数判断测试", number, res);
    }

    /**
     * 找到落单的数测试
     */
    private static void findSingularNumberTest() {
        //目标数组，数组元素个数为奇数，且只有一个数字只出现过一次
        int[] targetArray = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};//TODO 6 出现次数为一次
        Integer singularNumber = BitwiseOperation.findSingularNumber(targetArray);
        Tools.print("找到落单的数测试",
                "{1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1}",
                "落单的数为：" + singularNumber.toString());
    }

    /**
     * 找到成双的数测试
     */
    private static void findPairNumberTest() {
        //目标数组
        int[] targetArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4};//TODO 4 出现次数为两次
        Integer pairNumberByXOR = BitwiseOperation.findPairNumberByXOR(targetArray);
        Tools.print("找到成双的数测试-XOR",
                "{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4}",
                "XOR-成双的数字为：" + pairNumberByXOR);
        Integer pairNumberByHelperSpace = BitwiseOperation.findPairNumberByHelperSpace(targetArray);
        Tools.print("找到成双的数测试-HelperSpace",
                "{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4}",
                "HelperSpace-成双的数字为：" + pairNumberByHelperSpace);
    }
}
